package com.ymatou.iapi.im.testcase;

import com.google.common.reflect.TypeToken;
import com.ymatou.iapi.im.parameter.GetCSMessageListByTimelineBean;
import com.ymatou.iapi.im.parameter.SendMessageBean;
import com.ymatou.iapi.im.parameter.emu.MessageIsNewType;
import com.ymatou.iapi.im.service.GetCSMessageListByTimelineCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ImCallService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.YmtMessage;
import com.ymttest.database.model.YmtMessageAttach;
import com.ymttest.database.sqlwapper.ImDefaultWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.List;

/**
*洋管家获取私信消息和会话信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetCSMessageListByTimelineAction{
	private static GetCSMessageListByTimelineBean getcsmessagelistbytimelineBean;
	private static GetCSMessageListByTimelineCall getcsmessagelistbytimelineCall;
	private static int imId = Integer.parseInt(EnvSetup.getData("userId_IM"));
	private static String imName = EnvSetup.getData("userName_IM");
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("洋管家获取私信消息和会话信息");
	}

	@Before
	public void caseUp() {
		getcsmessagelistbytimelineBean=new GetCSMessageListByTimelineBean();
		getcsmessagelistbytimelineCall=new GetCSMessageListByTimelineCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getcsmessagelistbytimelineCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

    public void checkMessage(GetCSMessageListByTimelineBean getMessageListAndSessionBean) throws InterruptedException {
        ImDefaultWapper im = new ImDefaultWapper();
        int fromUserId = getMessageListAndSessionBean.getFromUserId();
        int toUserId = getMessageListAndSessionBean.getToUserId();
        List<YmtMessage> ymtMessageMap = im.selectMessageByToUserIdAndFromUserId(fromUserId,toUserId);
        Logger.debug("ymtMessageMap:"+ymtMessageMap);
        String ymtMessageList = getcsmessagelistbytimelineCall.getJsonObject().get("Result").getAsJsonArray().toString();
        List<com.ymatou.iapi.im.parameter.YmtMessage > ymtMessages = (List<com.ymatou.iapi.im.parameter.YmtMessage>)FormatUtil.GSONToObject(ymtMessageList, new TypeToken<List<com.ymatou.iapi.im.parameter.YmtMessage >>(){}.getType());
        Logger.debug("ymtMessages:"+ymtMessages);
        String messageId = null;
        for (int j=0;j<ymtMessageMap.size();j++){
            messageId = ymtMessageMap.get(j).getMsgFromId()+"_"+ymtMessageMap.get(j).getMsgToId()+"_"+ymtMessageMap.get(j).getMessageId();
            for (int i=0;i<ymtMessages.size();i++){
                if (messageId.equals(ymtMessages.get(i).getMessageId())){
                    Logger.verifyEquals(ymtMessages.get(i).getMsgFromId(),ymtMessageMap.get(j).getMsgFromId(),"验证fromUserId");
                    Logger.verifyEquals(ymtMessages.get(i).getMsgToId(),ymtMessageMap.get(j).getMsgToId(),"验证toUserId");
                    Logger.verifyEquals(ymtMessages.get(i).getMsgFrom(),ymtMessageMap.get(j).getMsgFrom(),"验证MsgFrom");
                    Logger.verifyEquals(ymtMessages.get(i).getMsgTo(),ymtMessageMap.get(j).getMsgTo(),"验证MsgTo");
                    Logger.verifyEquals(ymtMessages.get(i).getMessage(),ymtMessageMap.get(j).getMessage(),"验证Message");
                    Logger.verifyEquals(ymtMessages.get(i).getCsName(),ymtMessageMap.get(j).getCsName(),"验证CsName");
                    Logger.verifyEquals(ymtMessages.get(i).getSessionNumber(),ymtMessageMap.get(j).getSessionNumber(),"验证SessionNumber");
                    if (ymtMessages.get(i).isCancel()==true){
                        YmtMessageAttach ymtMessageAttach = im.selectMessageattachByMessageId(messageId).get(0);
                        Logger.verifyEquals(ymtMessages.get(i).getMessageAttachList().get(0).getContent(),ymtMessageAttach.getContent(),"验证MessageAttachList中的Content");
                        Logger.verifyEquals(ymtMessages.get(i).getMessageAttachList().get(0).getType(),ymtMessageAttach.getType(),"验证MessageAttachList中的type");
                    }
                }
            }
        }
    }

    /**
     * IsNotClearUnReadCount参数过期
     */
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCSMessageListByTimeline_01() {
		Logger.start(true,"洋管家获取私信消息和会话信息,isNew为yes");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			SendMessageBean sendMessageBean = ImCallService.initSendMessage(imId,userId,imName);
			ImCallService.SendMessage(sendMessageBean);
			//传入过去的时间,查得到消息
			getcsmessagelistbytimelineBean = ImCallService.initGetCSMessageListByTimeline(imId,userId);
			getcsmessagelistbytimelineCall.setData(getcsmessagelistbytimelineBean);
			getcsmessagelistbytimelineCall.callService();
			Logger.verifyEquals("Success",getcsmessagelistbytimelineCall.getString("Status"),"验证Status");
            checkMessage(getcsmessagelistbytimelineBean);
            //传入现在的时间，查不到消息
			getcsmessagelistbytimelineBean.setLongTimeline(YMTDateUtil.getDate().getTime());
            getcsmessagelistbytimelineCall.setData(getcsmessagelistbytimelineBean);
            getcsmessagelistbytimelineCall.callService();
            Logger.verifyEquals("Success",getcsmessagelistbytimelineCall.getString("Status"),"验证Status");
            Logger.verifyEquals("[]",getcsmessagelistbytimelineCall.getJsonObject().get("Result").toString(),"验证YmtMessageList为空");
        } catch (Exception e) {
			Logger.fail(e);
		}
	}

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetCSMessageListByTimeline_02() {
        Logger.start(true,"洋管家获取私信消息和会话信息,isNew为No");
        try {
            UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
            UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
            Integer userId = urrc.getUserid();
            SendMessageBean sendMessageBean = ImCallService.initSendMessage(imId,userId,imName);
            ImCallService.SendMessage(sendMessageBean);
            //传入过去的时间,查bu到消息
            getcsmessagelistbytimelineBean = ImCallService.initGetCSMessageListByTimeline(imId,userId);
            getcsmessagelistbytimelineBean.setIsNew(MessageIsNewType.No);
            getcsmessagelistbytimelineCall.setData(getcsmessagelistbytimelineBean);
            getcsmessagelistbytimelineCall.callService();
            Logger.verifyEquals("Success",getcsmessagelistbytimelineCall.getString("Status"),"验证Status");
            Logger.verifyEquals("[]",getcsmessagelistbytimelineCall.getJsonObject().get("Result").toString(),"验证YmtMessageList为空");

            //传入现在的时间，查de到消息
            getcsmessagelistbytimelineBean.setLongTimeline(YMTDateUtil.getDate().getTime());
            getcsmessagelistbytimelineCall.setData(getcsmessagelistbytimelineBean);
            getcsmessagelistbytimelineCall.callService();
            Logger.verifyEquals("Success",getcsmessagelistbytimelineCall.getString("Status"),"验证Status");
            checkMessage(getcsmessagelistbytimelineBean);
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

}

